﻿using WindowsForms.AOnline.Model.BusinessObjects.Dictionary;
using WindowsForms.AOnline.Presenter.Dictionary.Account;
using WindowsForms.AOnline.View.Dictionary;
//using System.Threading.Tasks;

namespace WindowsForms.AOnline.Presenter.Dictionary.Unit
{
    public class UnitPresenter : Presenter<IUnitView>  
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AccountPresenter" /> class.
        /// </summary>
        /// <param name="view">The view.</param>
        public UnitPresenter(IUnitView view)
            : base(view)
        {
        }

        /// <summary>
        /// Displays the specified unit identifier.
        /// </summary>
        /// <param name="unitId">The unit identifier.</param>
        public void Display(string unitId) 
        {
            if (unitId == null) { View.UnitId = 0; return; }
            var unit = Model.GetUnit(int.Parse(unitId));
            //View.EmployId = unit.EmployId;
            View.UnitId = unit.UnitId;
            View.ParentId = unit.ParentId;
            View.Code = unit.Code;
            View.UnitName = unit.Name;
            View.Grade = unit.Grade;
            View.IsActive = unit.IsActive;
            View.HasParent = unit.HasParent;
            View.PhoneNumber = unit.PhoneNumber;
            View.Address = unit.Address;
            View.ByManagement = unit.ByManagement;
        }

        /// <summary>
        /// Saves this instance.
        /// </summary>
        /// <returns>System.Int32.</returns>
        public string Save()
        {
            var unit = new UnitModel    
            {
            UnitId = View.UnitId,
            ParentId = View.ParentId,
            Code = View.Code,
            Name = View.UnitName,
            Grade = View.Grade,
            IsActive = View.IsActive,
            HasParent = View.HasParent,
            PhoneNumber = View.PhoneNumber,
            Address = View.Address,
            ByManagement = View.ByManagement,
            };

            if (View.UnitId == 0)
                return Model.AddUnit(unit);
            return Model.UpdateUnit(unit);
        }

        /// <summary>
        /// Deletes the specified unit identifier.
        /// </summary>
        /// <param name="unitId">The unit identifier.</param>
        /// <returns>System.Int32.</returns>
        public string Delete(int unitId) 
        {
            return Model.DeleteUnit(unitId);
        }
    }
}
